#include<iostream>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<map>
#include<vector>
using namespace std;
typedef long long LL;
#define int long long
const int N = 2e5 + 10;
int n, q;
int a[N];
map<int, int>mp;
vector<int>ans;
signed main()
{
	int t;
	cin >> t;
	while (t--) {
		cin >> n;
		mp.clear();
		ans.clear();
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
			mp[a[i]]++;
		}
		cin >> q;
		while (q--) {
			int x, y;
			cin >> x >> y;
			int d = x * x - 4 * y;
			int r = sqrt(x * x - 4 * y);
			if (r * r != d) {
				ans.push_back(0);
				continue;
			}
			int x1 = (x + r) / 2;
			int x2 = (x - r) / 2;
			if (x1 == x2) ans.push_back(mp[x1] * (mp[x1] - 1) / 2);
			else ans.push_back(mp[x1] * mp[x2]);
		}
		for (int x : ans) {
			cout << x << " ";
		}
		cout << endl;
	}
	return 0;
}